mini.parse();

function AgentFormHandler() {
};

AgentFormHandler.instance = new AgentFormHandler();

//获得全局的代理商表单对象
AgentFormHandler.agentForm = new mini.Form("agentForm");
AgentFormHandler.formData = {};

// 代理商状态数据
AgentFormHandler.baiStateData = [
	{text: "有效",value: 1},
	{text: "无效",value: 0}
];
mini.get("baiState").setData(AgentFormHandler.baiStateData);

/**
 * 保存（添加或编辑）数据的方法
 */
AgentFormHandler.save = function(){
	var agentData = AgentFormHandler.agentForm.getData();
	agentData.baiId = AgentFormHandler.formData.baiId;
	
    AgentFormHandler.agentForm.validate();
    if (AgentFormHandler.agentForm.isValid() == false) return;
    
    var json = mini.encode(agentData);
    
    $.ajax({
        url: __root + "/saveOrUpdateAgentInfo.do",
        type: "POST",
        data: {
        	agentInfo: json,
        	lslUser: gbOperateUser,
    		lslAddress: gbOperateIp
        },
        cache: false,
        success: function (text) {
        	MiniuiTools.miniInfo("系统提示","保存成功！", function(action){
        		AgentFormHandler.closeCurrentWin("save");
        	});
        },
        error: function (jqXHR, textStatus, errorThrown) {
        	MiniuiTools.miniError("系统提示", jqXHR.responseText, function(){
            	AgentFormHandler.closeCurrentWin();
            });
        }
    });
    
}

/**
 * 标准方法接口定义
 * @param {} data
 */
AgentFormHandler.passData = function(data) {
    if (data.action == "edit") {
        //跨页面传递的数据对象，克隆后才可以安全使用
        data = mini.clone(data);
        AgentFormHandler.formData.baiId = data.baiId;
        //根据id获取对应的代理商信息
        $.ajax({ 
            url: __root + "/getAgentInfoById.do?baiId=" + data.baiId,
            cache: false,
            dataType: 'json',
            success: function (text) {
                AgentFormHandler.agentForm.setData(text);
            }
        });
    }else{
    	//默认代理商状态为有效
    	mini.get("baiState").setValue(1);
    }
}

/**
 * 关闭窗口
 * @param {} action
 */
AgentFormHandler.closeCurrentWin = function(action) {
    if (window.CloseOwnerWindow) window.CloseOwnerWindow(action);
    else window.close();
}

AgentFormHandler.closeMiniWin = function(){
	AgentFormHandler.closeCurrentWin("cancel");
}